<?php
declare(strict_types=1);

namespace app\validate\api\admin\system;

use app\validate\api\ApiBaseValidate;

class UserValidate extends ApiBaseValidate
{
    protected $regex = [
        'ids' => '^\d+(,\d+)*',
        'password' => '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,32}$'
    ];

    protected $ex_data = [];

    protected $rule = [
        'id' => 'require|integer|notSelf',
        'username' => 'require|unique:admin',
        'email' => 'email',
        'phone' => 'mobile',
        'password' => 'require|alphaNum|min:8|max:32|regex:password',
        // 'salt' => '',
        'status' => 'integer|in:0,1',
        'pid' => 'integer',
        // 'pids' => 'max:255',
        'avatar' => 'max:255',
        // 'token' => '',
        // 'login_ip' => '',
        // 'login_nums' => '',
        // 'login_time' => '',
        // 'login_fail_num' => '',
        // 'login_fail_time' => '',
        'dept_id' => 'integer',
        'nickname' => 'max:64',
        'sex' => 'integer|in:1,2',
        'description' => 'max:255',
        'remark' => 'max:255',


        // 扩展
        'del_ids' => 'require|regex:ids',
        'role_ids' => 'regex:ids',
        'old_password' => 'require|alphaNum|min:8|max:32',
    ];

    protected $message = [
        'password.regex' => '密码必须是8-32位数字和字母组合(大小写字母)',
    ];

    protected $scene = [
    ];

    public function sceneSave()
    {
        return $this->only([
            'username',
            'email',
            'phone',
            'password',
            'status',
            'pid',
            'avatar',
            'dept_id',
            'nickname',
            'sex',
            'description',
            'remark',
            'role_ids'
        ])
            ->append('dept_id', 'require');
    }
    public function sceneUpdate()
    {
        return $this->only([
            'id',
            'username',
            'email',
            'phone',
            'password',
            'status',
            'pid',
            'avatar',
            'dept_id',
            'nickname',
            'sex',
            'description',
            'remark',
            'role_ids',
        ])
            ->remove('password', 'require');
    }

    public function sceneDelete()
    {
        return $this->only([
            'del_ids',
        ])->append('del_ids', 'notSelf');
    }

    public function notSelf($value, $rule, $data)
    {
        $ids = [];
        isset($data['id']) && $ids[] = $data['id'];
        if (isset($data['del_ids'])) {
            $ids = array_merge($ids, explode(',', $data['del_ids']));
        }
        if (in_array(request()->x_token_user['id'], $ids) && !is_super_admin()) {
            return '不能修改/删除自己,请联系上级管理员';
        }
        return true;
    }


    public function sceneMineUpdate()
    {
        return $this->only([
            'id',
            'username',
            'email',
            'phone',
            'avatar',
            'nickname',
            'sex',
            'description',
        ])->remove('id', 'notSelf'); // id后端已定
    }

    public function sceneChangePwd()
    {
        return $this->only([
            'id',
            'old_password',
            'password',
        ])->remove('id', 'notSelf'); // id后端已定
    }
}
